package com.jin.crud.controller;


import com.jin.crud.bean.QueryMenu;
import com.jin.crud.bean.SysMenu;
import com.jin.crud.common.utils.ResultUtil;
import com.jin.crud.common.utils.SecurityUtils;
import com.jin.crud.service.SysMenuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author yuxuejin
 * @since 2021-11-20
 */
@RestController
@RequestMapping("/system/menu")
public class SysMenuController {

    @Autowired
    SysMenuService menuService;

    @ApiOperation("查询菜单")
    @GetMapping("/listMenu")
    public ResultUtil<List<SysMenu>> getMenuList(QueryMenu menu) {
        String userId = SecurityUtils.getLoginUser().getUserId();
        return ResultUtil.resultSuccess(menuService.selectMenuList(menu, userId));
    }

    @ApiOperation("根据菜单编号获取详细信息")
    @GetMapping(value = "/getMenu")
    public ResultUtil getMenu(String menuId) {
        return ResultUtil.resultSuccess(menuService.selectMenuById(menuId));
    }

    @ApiOperation("添加菜单")
    @PostMapping("/addMenu")
    public ResultUtil addMenu(SysMenu menu) {
        menuService.addMenu(menu);
        return ResultUtil.resultSuccess();
    }

    @ApiOperation("删除菜单")
    @PostMapping("/delMenu")
    public ResultUtil delMenu(String menuId) {
        menuService.delMenu(menuId);
        return ResultUtil.resultSuccess();
    }

    @ApiOperation("更新菜单")
    @PostMapping("/updateMenu")
    public ResultUtil updateMenu(SysMenu menu) {
        menuService.updateMenu(menu);
        return ResultUtil.resultSuccess();
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeSelect")
    public ResultUtil treeSelect(QueryMenu menu) {
        String userId = SecurityUtils.getLoginUser().getUserId();
        List<SysMenu> menus = menuService.selectMenuList(menu, userId);
        return null;
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    public ResultUtil treeselect(QueryMenu menu) {
        String userId = SecurityUtils.getLoginUser().getUserId();
        List<SysMenu> menus = menuService.selectMenuList(menu, userId);
        return ResultUtil.resultSuccess(menuService.buildMenuTreeSelect(menus));
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeselect")
    public ResultUtil roleMenuTreeSelect(String roleId) {
        String userId = SecurityUtils.getLoginUser().getUserId();
        List<SysMenu> menus = menuService.selectMenuList(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
        map.put("menus", menuService.buildMenuTreeSelect(menus));
        return ResultUtil.resultSuccess(map);
    }

}
